//
//  NIDiagnosisView.m
//  gweAD
//
//  Created by Navinfo on 2017/7/20.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NIDiagnosisView.h"

@implementation NIDiagnosisView{
    
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UIView *animationView;
    
    
    __weak IBOutlet UILabel *workinglabel;
    
    __weak IBOutlet UILabel *scoreLabel;
    CALayer *circleAnimationlayer;
    CALayer *circleLayer;
    __weak IBOutlet UIButton *checkButton;
    __weak IBOutlet UIButton *recheckButton;
    __weak IBOutlet UILabel *recheckLabel;
    
    float score;
    
    SEL selector;
    SEL reselector;
}
- (IBAction)onCheckButtonClick:(id)sender {
    if(((UIButton *)sender).tag == 101){
        if (self.delegate && selector) {
            [self.delegate performSelector:selector];
        }
    }else{
        if (self.delegate && reselector) {
            [self.delegate performSelector:reselector];
        }
    }
    [self toCheckingState];
    
}

- (void)setSelector:(SEL)theSelector{
    selector = theSelector;
}

- (void)setSelectorForRecheck:(SEL)theSelector{
    reselector = theSelector;
}
-(void)awakeFromNib
{
    
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
}

- (void)toCheckingState{
    [self removeAllLayers];
    [self startAnimation];
}

- (void)toResultState:(float)theScore showCheckButton:(BOOL)show{
    [self removeAllLayers];

    [self setScore:theScore];
    
    recheckButton.hidden = !show;
    recheckLabel.hidden = !show;
}

- (void)toInitState{
    [self removeAllLayers];
    
    checkButton.hidden = NO;
}

- (void)removeAllLayers{
    if (circleAnimationlayer) {
        [circleAnimationlayer removeAllAnimations];
        [circleAnimationlayer removeFromSuperlayer];
    }
    
    if (circleLayer) {
        [circleLayer removeFromSuperlayer];
    }
    
    scoreLabel.hidden = YES;
    workinglabel.hidden = YES;
    checkButton.hidden = YES;
    recheckButton.hidden = YES;
    recheckLabel.hidden = YES;
}

- (void)stopAnimation{
}

- (void)startAnimation{
    workinglabel.hidden = NO;
    
    CGFloat rectWidth = [checkButton imageForState:UIControlStateNormal].size.width;
    CGFloat lineWidth = 3.0f;
    CGFloat radius = (rectWidth - lineWidth)/2;
    
    circleAnimationlayer = [CALayer layer];
    circleAnimationlayer.frame = CGRectMake(0, 0, rectWidth, rectWidth);
    circleAnimationlayer.backgroundColor = [UIColor redColor].CGColor;
    
    CALayer *blackLayer = [CALayer layer];
    blackLayer.frame = CGRectMake(rectWidth/2, rectWidth/2, rectWidth/2, rectWidth/2);
    blackLayer.backgroundColor = [UIColor blackColor].CGColor;
    [circleAnimationlayer addSublayer:blackLayer];
    
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(rectWidth/2, rectWidth/2) radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.strokeStart = 0;
    shapeLayer.strokeEnd = 0.995;
    shapeLayer.lineCap = @"round";
    shapeLayer.path = bezierPath.CGPath;
    [circleAnimationlayer setMask:shapeLayer];
    
    NSMutableArray *colors = [NSMutableArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColor blackColor].CGColor, nil];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.shadowPath = bezierPath.CGPath;
    gradientLayer.frame = CGRectMake(0, rectWidth/2, rectWidth/2, rectWidth/2);
    [gradientLayer setColors:[NSArray arrayWithArray:colors]];
    [circleAnimationlayer addSublayer:gradientLayer];
    
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 1.5f;
    animation.delegate = self;
    animation.repeatCount = MAXFLOAT;
    animation.fromValue = [NSNumber numberWithFloat:0];
    animation.toValue = [NSNumber numberWithFloat:M_PI *2];
    animation.removedOnCompletion = NO;
    [circleAnimationlayer addAnimation:animation forKey:@"diagnosisAnimation"];
    animationView.backgroundColor = [UIColor clearColor];
    [animationView.layer addSublayer:circleAnimationlayer];
}


- (void)setScore:(float)theScore{
    scoreLabel.hidden = NO;
    score = floor(theScore);
    
    CGFloat rectWidth = [checkButton imageForState:UIControlStateNormal].size.width;
    CGFloat lineWidth = 3.0f;
    CGFloat radius = (rectWidth - lineWidth)/2;
    
    UIColor *displayColor = [UIColor whiteColor];
    if (score < 80) {
        displayColor = [UIColor redColor];;
    }else if (score < 99){
        displayColor = [UIColor redColor];
    }else{
        displayColor = [UIColor redColor];
    }
    displayColor = [UIColor redColor];
    [scoreLabel setBackgroundColor:[UIColor clearColor]];
    NSString *value = [NSString stringWithFormat:@"%.f",score];
    NSString *unit = @"分";
    NSString *text = [NSString stringWithFormat:@"    %@  %@",value,unit];
    UIFont *valueFont = [UIFont systemFontOfSize:48];
    UIFont *unitFont = [UIFont systemFontOfSize:20];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:text];
    
    NSRange unitRange = [text rangeOfString:unit];
    NSRange valueRange = [text rangeOfString:value];
    
    [AttributedStr addAttribute:NSFontAttributeName
                          value:valueFont
                          range:valueRange];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:displayColor
                          range:valueRange];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:unitFont
                          range:unitRange];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:displayColor
                          range:unitRange];
    
    scoreLabel.attributedText = AttributedStr;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(rectWidth/2, rectWidth/2) radius:radius startAngle:M_PI*3/2 endAngle:M_PI*2 + M_PI*3/2 clockwise:YES];
    
    circleLayer = [CALayer layer];
    circleLayer.frame = CGRectMake(0, 0, rectWidth, rectWidth);
    circleLayer.backgroundColor = displayColor.CGColor;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = displayColor.CGColor;
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.strokeStart = 0;
    shapeLayer.strokeEnd = score/100.0f;
    shapeLayer.lineCap = @"round";
    shapeLayer.path = bezierPath.CGPath;
    [circleLayer setMask:shapeLayer];
    
    
    CALayer *bgcircleLayer = [CALayer layer];
    bgcircleLayer.frame = CGRectMake(0, 0, rectWidth, rectWidth);
    bgcircleLayer.backgroundColor = [UIColor blackColor].CGColor;
    
    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
    shapeLayer2.fillColor = [UIColor clearColor].CGColor;
    shapeLayer2.strokeColor = displayColor.CGColor;
    shapeLayer2.lineWidth = lineWidth;
    shapeLayer2.strokeStart = 0;
    shapeLayer2.strokeEnd = 1.0f;
    shapeLayer2.lineCap = @"round";
    shapeLayer2.path = bezierPath.CGPath;
    [bgcircleLayer setMask:shapeLayer2];
    
    
    animationView.backgroundColor = [UIColor clearColor];
    [animationView.layer addSublayer:bgcircleLayer];
    [animationView.layer addSublayer:circleLayer];
    
}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
//    [self stopLockUnlockAnimation];
//    if (runningAnimationType == NIVehicleAnimationTypeLock) {
//        lock1ImageView.hidden = YES;
//        lock2ImageView.hidden = NO;
//        circleAnimationView.hidden = YES;
//    }else{
//        unlock1ImageView.hidden = YES;
//        unlock2ImageView.hidden = NO;
//        circleAnimationView.hidden = YES;
//    }
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self reloadData];
//        runningAnimationType = NIVehicleAnimationTypeNone;
//    });
}

@end
